//sendAjax2 this includes queing
/*############################################
sendAjax(method, url, qs, successFunc, failureFunc, debug)
sendAjax('GET', '/?val=2&soandso=5', 'otherval=3', whatToDoWhenSuccess, whatToDoWhenFailure, false)
sendAjax('POST', '/', 'val=2&soandso=5&otherval=3', null, null, true)
############################################*/
function AjaxObj(method, url, qs, success, failure, debug) {
	var self = this;
	
	url += (url.indexOf("?") > 0) ? "&" : "?";
	if (method.toLowerCase() == "get") {
		if (qs) {
			url += qs + '&';
		}
		qs = (window.XMLHttpRequest) ? null : '';
	}
	
	url += "clocktime=" + new Date().getTime();
	url = window.location.href.substr(0, window.location.href.indexOf('/', 8)) + url;
	self.method = method;
	self.url = url;
	self.qs = qs;
	self.success = success;
	self.failure = failure;
	self.debug = debug ? true : false;
	self.ajaxHttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		self.ajaxHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			self.ajaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			self.ajaxHttp = false;
		}
	}
	@end @*/

	if (!self.ajaxHttp && typeof(XMLHttpRequest) != 'undefined') {
		self.ajaxHttp = new XMLHttpRequest();
	}

	self.handleAjaxResponse = function() {
		if (self.ajaxHttp.readyState == 4) {
			var ajaxResponse = self.ajaxHttp.responseText;
			var status = self.ajaxHttp.status;
			var debug = self.debug;
			var success = self.success;
			var failure = self.failure;

			if (debug) {
				self.ajaxDebugCallback(ajaxResponse, status);
			}
			else {
				if (status == 200) {
					if (typeof(success) == 'function')
						success(ajaxResponse, status);
				}
				else if (status != 0 && typeof(failure) == 'function'){
					failure(ajaxResponse, status);
				}
			}
		}
	}

	self.ajaxDebugCallback = function(result, status) {
		var win = window.open();
		if (status == 200) {
			win.document.body.innerHTML = "Success with URI: " + self.url + "<p>" + result;
		}
		else {
			win.document.body.innerHTML = "Failure with URI: " + self.url + " (error " + status + ")<p>" + result;
		}
	}
	
	self.ajaxHttp.onreadystatechange = self.handleAjaxResponse;
}

function sendAjax(method, url, qs, successFunc, failureFunc, debug) {
	var Next_Ajax = new AjaxObj(method, url, qs, successFunc, failureFunc, debug);
	Next_Ajax.ajaxHttp.open(Next_Ajax.method, Next_Ajax.url, true);
	if (method.toLowerCase() == "post") {
		Next_Ajax.ajaxHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	}
	
	try {
		Next_Ajax.ajaxHttp.send(Next_Ajax.qs);
	}
	catch (err) {
		return false;
	}

	// Hopefully this prevents memory leaks
	delete(Next_Ajax);
	Next_Ajax = null;

	return true;
}



function throwAjaxError(result, status) {
	alert('An exception occured during your request.');
}